﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using CoreShop.Model;
using CoreShop.Services;
using Sunny.UI;
using CoreShop.Library;
using System.Linq;
using SqlSugar.Extensions;
using System.Diagnostics.SymbolStore;
using System.Security.Cryptography;

namespace CoreShop.App
{
    public partial class frmFoodSpecSelect : UIForm
    {
        public List<corecmsfoodspec> specList = new List<corecmsfoodspec>();
        public corecmsfoodspec specModel = new corecmsfoodspec();
        public bool _setDisabled;

        public frmFoodSpecSelect(List<corecmsfoodspec> _foodspecs, corecmsfood _corecmsfood, bool setDisabled = false)
        {
            specList = _foodspecs;
            InitializeComponent();
            _setDisabled = setDisabled;
        }

        private void frmTableOpen_Load(object sender, EventArgs e)
        {
            if (specList.Count > 0)
            {
                //获取调价单
                var spuIds = specList.Select(f => f.id).ToList();
                var listPrice = new corecmschgpriceServices().GetChgPriceList(spuIds);
                if (listPrice != null && listPrice.Count > 0)
                {
                    foreach (var item in specList)
                    {
                        var findItem = listPrice.Find(p => p.pdtSpuSpecId == item.id);
                        if (findItem != null) item.posPrice = findItem.newSalePrice;
                    }
                }
            }
            BindSpecList();
        }

        /// <summary>
        /// 商品按钮名称
        /// </summary>
        /// <param name="deskId"></param>
        /// <returns></returns>
        private string GetFoodItemName(int index)
        {
            return "uiFoodItem_" + index.ToString();
        }

        private void BindSpecList()
        {
            var soldoutList = new corecmsfoodsoldoutServices().AsQueryable().Where(p => p.foodId == specList[0].foodId).ToList();
            fly_foodlist.Visible = false;
            this.fly_foodlist.SuspendLayout();// 添加很多时挂起会更快（不触发界面更改重新绘图事件）
            int index = 1;
            foreach (var food in specList)
            {
                DishItemInfo dish = new DishItemInfo();
                dish.Name = food.id.ToString();
                dish.Money = food.posPrice.ToString();
                dish.UnitName = food.specUnitName;
                dish.LabelExist = food.specName;
                var item = new UIDishItemSpec(dish);
                item.Tag = food.id.ToString();
                item.TextName = food.name;
                item.Name = GetFoodItemName(index);
                item.Margin = new System.Windows.Forms.Padding(3);
                item.Click += Item_Click;
                item.SetEnabled();

                var soldoutItem = soldoutList.Find(p => p.foodSpecId == food.id);
                if (soldoutItem != null)
                {
                    dish.LabelExist = string.Format("剩{0}", CommFunc.FormatDecimal(soldoutItem.soldout));
                    if (soldoutItem.soldout == 0 && _setDisabled)
                    {
                        item.SetDisabled();
                        item.FillDisableColor=Color.LightGray;
                    }
                     
                }

                fly_foodlist.Add(item);
                index++;
            }
            this.fly_foodlist.ResumeLayout();// 添加很多时挂起会更快（不触发界面更改重新绘图事件）
            fly_foodlist.Visible = true;
            fly_foodlist.Refresh();
        }

        private void Item_Click(object sender, System.EventArgs e)
        {
            UIDishItemSpec item = (UIDishItemSpec)sender;
            specModel = specList.Find(p => p.id == CommFunc.ConvertToBigInt(item.Tag));
            if (specModel != null)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            //AddDataRow(CommFunc.ConvertToBigInt(item.Tag), 1);
        }
    }
}
